package com.compass.doctor.ui.patient

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.databinding.ActivityDiagnoseBinding
import com.compass.doctor.dialog.AppendDiagnoseDialog
import com.compass.doctor.ui.home.adapter.ExecutiveDepartmentAdapter
import com.compass.doctor.ui.patient.viewmodel.ExamineViewModel
import com.compass.doctor.ui.prescription.adapter.DiagnoseAdapter
import com.compass.doctor.ui.prescription.viewmodel.PrescribeViewModel
import com.compass.framework.constant.KEY_CONSULT_TYPE
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.utils.dpToPx

/**
 * Description： 执行科室列表
 * Date:2024/3/7 14:51
 */
class ExecutiveDepartmentActivity : BaseVBActivity<ActivityDiagnoseBinding>() {


    private val examineViewModel: ExamineViewModel by viewModels()
    private lateinit var mAdapter: ExecutiveDepartmentAdapter
//    InpatientWard

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.tvTitleBar.setMiddleText("执行科室")
        mBinding.edtDiagnosis.hint="请输入科室关键字"

        mBinding.edtDiagnosis.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val keyWord = mBinding.edtDiagnosis.text.toString().trim()
                showLoading()
                getDiagnosisInfo(keyWord)
            }
            false
        }
        mBinding.tvTitleBar.getRightLayout().onClick {
            AppendDiagnoseDialog.Builder(this).show()
        }

        mBinding.btnSearch.onClick {
            val keyWord = mBinding.edtDiagnosis.text.toString().trim()
            showLoading()
            getDiagnosisInfo(keyWord)
        }
        initRecyclerView()
    }

    override fun initData() {
        getDiagnosisInfo()
        examineViewModel.departmentsLiveData.observe(this) {
            dismissLoading()
            if (it.isNullOrEmpty()){
                mBinding.emptyView.visible()
                mAdapter.setData(null)
            }else{
                mBinding.emptyView.gone()
                mAdapter.setData(it)
            }
        }

    }

    /**
     * 获取科室列表
     */
    private fun getDiagnosisInfo(keyword: String = "") {
        showLoading()
        examineViewModel.searchDepartment(keyword)

    }


    private fun initRecyclerView() {
        mBinding.refreshLayout.apply {
            setEnableRefresh(false)
            setEnableLoadMore(false)

        }
        mAdapter = ExecutiveDepartmentAdapter()
        val dp1 = dpToPx(1)
        mBinding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(top = dp1, bottom = dp1)
                setLastBottom(true)

            })
            adapter = mAdapter
        }
        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)
            item?.let {
                intent.putExtra("department",item)
                setResult(RESULT_OK, intent)
                finish()
            }

        }

    }

}